package com.xiaomazi.security.entity;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: llxy-parent
 * @description: 用于封装返回的UserDetails
 * @author: 小马子
 * @create: 2023-03-14 12:33
 **/
@Data
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    private User user;

    private List<String> permissions;

    @JsonIgnore
    private List<SimpleGrantedAuthority> authorities;

    public SecurityUser(User user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Objects.isNull(authorities) ? permissions.stream()
                        .map(SimpleGrantedAuthority::new)
                        .collect(Collectors.toList()) : authorities;
    }


    @Override
    @JsonIgnore
    public String getPassword() {
        return user.getPassword();
    }


    @Override
    @JsonIgnore
    public String getUsername() {
        return user.getUsername();
    }


    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }


    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
